﻿Imports System.Data

Partial Class str_Default
    Inherits System.Web.UI.Page

#Region "SQL Commands"
    'Parent
    Function AddParent() As Boolean
        Try
            sqlparent.Insert()
            Return True
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Red, lblParentReturn)
            Return False
        End Try
    End Function
    Function CheckItem() As Boolean
        Dim dv As DataView = Nothing
        Try
            dv = CType(sqlParentCheck.Select(DataSourceSelectArguments.Empty), DataView)
            If dv.Count < 1 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            WriteParentError(ex.ToString, Drawing.Color.Red, lblParentReturn)
            Return False
        End Try
    End Function

    'Items
    Function AddItem() As Boolean
        'Add
        Try
            SqlItems.Insert()
            WriteParentError("Added", Drawing.Color.Black, returnItem)

            'Add Item to Table
            SqlItems.Select(DataSourceSelectArguments.Empty)
            ClearBox()
            Return True
        Catch ex As Exception
            WriteParentError(ex.ToString, Drawing.Color.Red, returnItem)
            Return False
        End Try
    End Function
    'All
    Sub DeleteTxn()
        Try
            sqlparent.Delete()
            SqlItems.Delete()
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Red, lblParentReturn)
        End Try
    End Sub

#End Region
#Region "Misc"
    Sub WriteParentError(statement As String, ForeColor As Drawing.Color, labeltarget As Label, Optional focusoncontrol As Control = Nothing)
        Try
            If labeltarget Is lblParentReturn Then
                If ForeColor = Drawing.Color.Red Then
                    labeltarget.CssClass = "ErrorLogs"
                Else
                    lblParentReturn.CssClass = "SuccessLogs"
                End If
                ForeColor = Drawing.Color.Black
            End If
            focusoncontrol.Focus()
        Catch ex As Exception

        End Try
        labeltarget.ForeColor = ForeColor
        labeltarget.Text = statement
    End Sub
    Sub ClearReturnbox()
        qtyReturn.Text = ""
        SKUReturn.Text = ""
        DescReturn.Text = ""
        UnitPriceReturn.Text = ""
        returnItem.Text = ""
    End Sub
    Sub ClearBox()
        txtQty.Text = ""
        txtSKU.Text = ""
        txtDescription.Text = ""
        txtUnitPrice.Text = ""
        Session("TotalPrice") = ""
    End Sub
    Sub ClearParent()
        txtStr_no.Text = ""
        txtTransfer_from.Text = ""
        txtTransfer_To.Text = ""
        txtPrepared_by.Text = ""
        txtApproved_by.Text = ""
        txt_Recievedby.Text = ""

    End Sub
    Sub DoneParent()
        ParentPanel.Enabled = False
        ItemPanel.Enabled = True
    End Sub
    Sub DoneItem()
        ParentPanel.Enabled = True
        ItemPanel.Enabled = False
    End Sub
#End Region
    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If (RadioButton1.Checked) Then
            Session("sel_company") = RadioButton1.Text
        ElseIf (RadioButton2.Checked) Then
            Session("sel_company") = RadioButton2.Text
        ElseIf (RadioButton3.Checked) Then
            Session("sel_company") = RadioButton3.Text
        End If
        
        Session("time_stamp") = Date.Now.ToString
        Session("agent") = User.Identity.Name.ToString

        If (txtStr_no.Text = "") Then
            Return
        End If

        If (txtTransfer_from.Text = "") Then
            Return
        End If

        If (txtTransfer_To.Text = "") Then
            Return
        End If

        If (txtPrepared_by.Text = "") Then
            Return
        End If

        If (txtApproved_by.Text = "") Then
            Return
        End If
        If (CalDateFiled.Text = "") Then
            Return
        End If
        'Check Duplicate
        If (CheckItem() = True) Then
            lblParentReturn.CssClass = "ErrorLogs"
            WriteParentError("Duplicate Entry", Drawing.Color.Black, lblParentReturn)
            Return
        End If
        'Add
        Try
            If AddParent() = True Then
                lblParentReturn.CssClass = "SuccessLogs"
                WriteParentError("Saved!", Drawing.Color.Black, lblParentReturn)
                SqlItems.Select(DataSourceSelectArguments.Empty)
                DoneParent()
                Button4.Enabled = True
            End If
        Catch ex As Exception
            lblParentReturn.CssClass = "ErrorLogs"
            WriteParentError(ex.Message, Drawing.Color.Black, lblParentReturn)
        End Try
    End Sub
   

    Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'checklblItemReturn

        If (txtQty.Text = "") Then
            WriteParentError("QTY must be specify!", Drawing.Color.Red, qtyReturn, txtQty)
            Return
        End If
        If (txtSKU.Text = "") Then
            WriteParentError("SKU must be specify!", Drawing.Color.Red, SKUReturn, txtSKU)
            Return
        End If
        If (txtDescription.Text = "") Then
            WriteParentError("Description must be specify!", Drawing.Color.Red, DescReturn, txtDescription)
            Return
        End If
        If (txtUnitPrice.Text = "") Then
            WriteParentError("Unit Price must be specify!", Drawing.Color.Red, UnitPriceReturn, txtUnitPrice)
            Return
        End If
        Try
            Session("TotalPrice") = (txtQty.Text * txtUnitPrice.Text)
            AddItem()
        Catch ex As Exception
            WriteParentError(ex.ToString, Drawing.Color.Red, returnItem)
        End Try
    End Sub
    

    Protected Sub txtUnitPrice_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
        Try
            ClearReturnbox()
            txtTotalPrice.Text = (txtUnitPrice.Text * txtQty.Text)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
        ClearReturnbox()
    End Sub

    Protected Sub txtSKU_TextChanged(sender As Object, e As EventArgs) Handles txtSKU.TextChanged
        ClearReturnbox()
    End Sub

    Protected Sub txtDescription_TextChanged(sender As Object, e As EventArgs) Handles txtDescription.TextChanged
        ClearReturnbox()
    End Sub

    Protected Sub txtTotalPrice_TextChanged(sender As Object, e As EventArgs) Handles txtTotalPrice.TextChanged
        ClearReturnbox()
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ClearReturnbox()
        ClearBox()
        ClearParent()
        Response.Redirect(Request.Url.AbsoluteUri)
    End Sub


    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        'Delete All Fields
        Try
            DeleteTxn()
            Response.Redirect(Request.Url.AbsoluteUri)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        '  CalDateFiled.SelectedDate = Now.Date
    End Sub
End Class
